/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: TstUtils.java 549 2007-05-08 18:40:13Z jack.rybicki $
 */
package logmatcher;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.logging.Log;

import java.io.File;

import java.lang.reflect.Field;

import java.util.Iterator;


/**
 * Various test helpers.
 *
 * @version $Revision: 549 $
*/
public final class TstUtils {
    //~ Constructors -------------------------------------------------------------------------------------------------

/**
     * Creates a new TestUtils object.
     */
    private TstUtils() {
        super();
    }

    //~ Methods ------------------------------------------------------------------------------------------------------

    /**
     * Get value of private field in an object.
     *
     * @param target object to be introspected.
     * @param fieldName name of the (private) field.
     *
     * @return field value.
     *
     * @throws SecurityException if
     * @throws NoSuchFieldException if object do not contain such field.
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static Object getPrivateField(Object target, String fieldName)
        throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        Class c = target.getClass();
        Field f = c.getDeclaredField(fieldName);
        f.setAccessible(true);

        return f.get(target);
    }


    /**
     * Print configuration to specified logger.
     *
     * @param logger target Log instance.
     * @param config Configuration to be printed.
     */
    public static void printConfiguration(Log logger, Configuration config) {
        for (Iterator it = config.getKeys(); it.hasNext();) {
            String key = it.next().toString();
            logger.info(key + "=" + config.getProperty(key));
        }
    }


    /**
     * Deletes all files and subdirectories under dir. Returns true if all deletions were successful. If a
     * deletion fails, the method stops attempting to delete and returns false.
     *
     * @param dir DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();

            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));

                if (!success) {
                    return false;
                }
            }
        }

        // The directory is now empty so delete it
        return dir.delete();
    }
}
